import pandas as pd
import altair as alt
import geopandas as gpd
f = pd.read_csv('../data/population_trends.csv')
df = f[f["year"] == 2019]
df.head()
| region | year | rate | |
|---|---|---|---|
| 30 | Ukraine | 2019 | -6.6 |
| 61 | Crimea | 2019 | NaN |
| 92 | Vinnytsya | 2019 | -7.9 |
| 123 | Volyn | 2019 | -2.8 |
| 154 | Dnipropetrovs'k | 2019 | -8.9 |
ukraine = gpd.read_file('../data/ukraine.json')
ukraine = ukraine[["NAME_1", "geometry"]]
ukraine.columns = ['region', 'geometry']
ukraine.head()
| region | geometry | |
|---|---|---|
| 0 | Cherkasy | MULTIPOLYGON (((31.32614 48.74507, 31.31716 48... |
| 1 | Chernihiv | MULTIPOLYGON (((33.09283 50.50966, 33.09261 50... |
| 2 | Chernivtsi | MULTIPOLYGON (((24.93280 47.72794, 24.93301 47... |
| 3 | Crimea | MULTIPOLYGON (((33.79291 44.39153, 33.79465 44... |
| 4 | Dnipropetrovs'k | MULTIPOLYGON (((33.93176 47.48407, 33.92332 47... |
select_region = alt.selection_single(on = 'mouseover', fields = ['region'], nearest = False, empty = 'none')
plot1 = alt.Chart(ukraine).mark_geoshape().encode(
color=alt.Color(field='rate', type='quantitative',
scale=alt.Scale(scheme='redyellowgreen', reverse=True),
legend=alt.Legend(title='Population growth/shrinkage ate', orient='left')),
opacity = alt.condition(
select_region,
alt.value(0.8),
alt.value(0.5)
)
).transform_lookup(
lookup='region',
from_=alt.LookupData(df, 'region', ['rate'])
).add_selection(
select_region
)
df = f[f.region != 'Ukraine']
plot2 = alt.Chart(df).mark_line().encode(
x = alt.X(field='year', type='nominal'),
y = alt.Y(field='rate', type='quantitative'),
detail = 'region:N',
color=alt.condition(select_region, 'region:N', alt.value('gray')),
size=alt.condition(select_region, alt.value(4), alt.value(1)),
).add_selection(
select_region
)
alt.hconcat(plot1, plot2).properties(background = '#F9F9F9', padding = 25)